package hut.ai.team.object;

import hut.ai.team.screen.MainScreen;
import hut.ai.team.table.VanCo;


public class Ma  {
	public Ma() {
		super();
	}

	static QuanCo qTuong1;
	static QuanCo qTuong2;
	
	private static int[][][] score = {
            {
                {40, 35, 40, 40, 40, 40, 40, 35, 40}, 		/* RED */
                {40, 41, 42, 40, 20, 40, 42, 41, 40},
                {40, 42, 43, 40, 40, 40, 43, 42, 40},
                {40, 42, 43, 43, 43, 43, 43, 42, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 41, 42, 42, 42, 42, 42, 41, 40},
                {40, 40, 40, 40, 40, 40, 40, 40, 40}
            },

            {
                {40, 40, 40, 40, 40, 40, 40, 40, 40}, 		/* BLACK */
                {40, 41, 42, 42, 42, 42, 42, 41, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 43, 44, 44, 44, 44, 44, 43, 40},
                {40, 42, 43, 43, 43, 43, 43, 42, 40},
                {40, 42, 43, 40, 40, 40, 43, 42, 40},
                {40, 41, 42, 40, 20, 40, 42, 41, 40},
                {40, 35, 40, 40, 40, 40, 40, 35, 40}
            }
         };
	
	public static int getDiemMa(QuanCo q){
		int d = 0;
		if(q.Phe == 2){
			if(q.TrangThai == 1)
				d = 10 + score[1][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 40 + score[1][q.Hang][q.Cot];
			else d = 0;
		}else{
			if(q.TrangThai == 1)
				d = 10 + score[0][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 40 + score[0][q.Hang][q.Cot];
			else
				d = 0;
		}
		return d;
	}
	
	public static void getQuanTuong(){
		if(MainScreen.getInstance().mode == 2){
			for(int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++){
				if(MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1){
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for(int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++){
				if(MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 1){
					qTuong2 = MainScreen.getInstance().n2.dsQuanCo.get(i);
					break;
				}
			}
		}else{
			for(int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++){
				if(MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1){
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for(int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++){
				if(MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 1){
					qTuong2 = MainScreen.getInstance().computer.dsQuanCo.get(i);
					break;
				}
			}
		}
	}
	
	public static int KiemTra(QuanCo c,int i, int j) {
		boolean turn = false;
		getQuanTuong();
        if (i == c.Hang - 2 && (j == c.Cot - 1 || j == c.Cot + 1))
            if (c.Hang - 2 >= 0 && c.Hang - 2 <= 9 && ((c.Cot - 1 >= 0 && c.Cot - 1 <= 8) || (c.Cot + 1 >= 0 && c.Cot + 1 <= 8)))
                if (MainScreen.getInstance().mBanCo.ViTri[c.Hang - 1][c.Cot].Trong == true)
                {
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true) turn = true;
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
                        if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe) turn = true;
                }
        if (i == c.Hang + 2 && (j == c.Cot - 1 || j == c.Cot + 1))
            if (c.Hang + 2 >= 0 && c.Hang + 2 <= 9 && ((c.Cot - 1 >= 0 && c.Cot - 1 <= 8) || (c.Cot + 1 >= 0 && c.Cot + 1 <= 8)))
                if (MainScreen.getInstance().mBanCo.ViTri[c.Hang + 1][c.Cot].Trong == true)
                {
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true) turn = true;
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
                        if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe) turn = true;
                }
        if (j == c.Cot - 2 && (i == c.Hang - 1 || i == c.Hang + 1))
            if (c.Cot - 2 >= 0 && c.Cot - 2 <= 8 && ((c.Hang - 1 >= 0 && c.Hang - 1 <= 9) || (c.Hang + 1 >= 0 && c.Hang + 1 <= 9)))
                if (MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot - 1].Trong == true)
                {
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true) turn = true;
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
                        if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe) turn = true;
                }
        if (j == c.Cot + 2 && (i == c.Hang - 1 || i == c.Hang + 1))
            if (c.Cot + 2 >= 0 && c.Cot + 2 <= 8 && ((c.Hang - 1 >= 0 && c.Hang - 1 <= 9) || (c.Hang + 1 >= 0 && c.Hang + 1 <= 9)))
                if (MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot + 1].Trong == true)
                {
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true) turn = true;
                    if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
                        if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe) turn = true;
                }
     // Kiểm tra chống tướng
     		if (MainScreen.getInstance().mode == 2) {
     			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong2.Cot) {
     				int ct = 0;
     				if (j != c.Cot) {
     					MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
     					for (int t = qTuong1.Hang + 1; t < qTuong2.Hang; t++) {
     						if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
     							ct++;
     					}
     					if (ct == 0)
     						turn = false;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
     					if(MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
     						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
     					else
     						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
     				}
     			}
     		} else {
     			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong1.Cot) {
     				int ct = 0;
     				if (j != c.Cot) {
     					MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
     					for (int t = qTuong2.Hang + 1; t < qTuong1.Hang; t++) {
     						if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
     							ct++;
     					}
     					if (ct == 0)
     						turn = false;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
     					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
     					if(MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
     						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
     					else
     						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
     				}
     			}
     		}
    	if (turn)
    		return 1;
    	else
    		return 0;
	}
	
	public static int TuongAnToan(QuanCo c, int i, int j) {
		boolean turn = true;
		getQuanTuong();
		if (turn == true) {
			int ho, co, tempTrangThai, tempTTBiAn = 0;
			QuanCo temp = null;

			ho = c.Hang;
			co = c.Cot;
			tempTrangThai = c.TrangThai;
			if (MainScreen.getInstance().mode == 2) {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().n1.Phe) {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n2.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n2.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n2.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n2.dsQuanCo.get(k);
						}
					}
				}
			} else {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().computer.Phe) {
						for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().computer.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().computer.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().computer.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					}
				}
			}
			// Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
			MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
			MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = 2;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = c.Loai;
			if(temp != null){
				tempTTBiAn = temp.TrangThai;
				VanCo.AnQuanCo(temp);
			}
			
			c.Hang = i;
			c.Cot = j;
			c.TrangThai = 2;
			// Kiểm tra
			if (MainScreen.getInstance().mode == 2) {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			} else {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			}
			// Trả lại những gì đã giả sử ở trên ^^!
			c.Hang = ho;
			c.Cot = co;
			c.TrangThai = tempTrangThai;
			MainScreen.getInstance().mBanCo.OCoTrong(i, j);
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai = c.TrangThai;
			if(MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai == 1)
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.LoaiCL;
			else
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.Loai;
			if(temp != null){
				if (temp.Phe != 0) {
					temp.TrangThai = tempTTBiAn;
					VanCo.HoiSinhQuanCo(temp);
					MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = temp.Phe;
					MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = tempTTBiAn;
					if(tempTTBiAn == 1)
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.Loai;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}
}
